using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Xeml.Document;
using Mpi.Common.Collections;
using System.Collections;
using System.Collections.ObjectModel;
using ComponentFactory.Krypton.Toolkit;

namespace Xid.Controls
{
    public partial class SsC_GermPlasmCtrl : UserControl
    {
        // private string individualsPool;
        //private ObservationCollection observations;

        //public ObservationCollection Observations
        //{
        //    get { return observations; }
        //    set { observations = value; }
        //}

        private List<SsC_ObservationControl> obCtrls;

        public List<SsC_ObservationControl> ObservationControls
        {
            get { return obCtrls; }
            set { obCtrls = value; }
        }


        //IndividualsPoolCollection individualsPools;
        //public IndividualsPoolCollection IndividualPools
        //{
        //    get { return individualsPools; }
        //    //set 
        //    //{ 
        //    //    individualsPool = value;
        //    //    if (individualsPool != null)
        //    //    {
        //    //        label1.DataBindings.Add("Text", individualsPool, "GermPlasm", true, DataSourceUpdateMode.Never);
        //    //    }

        //    //}
        //}


        private void SetLabelContent()
        {
            if (individualsPools != null)
            {

                StringBuilder sb = new StringBuilder();
                kryptonGroup1.Panel.Controls.Clear();
                foreach (var item in individualsPools)
                {

                    KryptonLabel kl = new KryptonLabel();
                    kl.LabelStyle = LabelStyle.NormalPanel;
                    kl.Text = item.Value;
                    kl.Dock = DockStyle.Top;
                kryptonGroup1.Panel.Controls.Add(kl);
                    //sb.Append(item.Value);
                    //sb.Append("\r\n");
                }
                if (sb.Length > 0)
                    sb.Remove(sb.Length - 1, 1);
               
              //  this.kryptonLabel1.Text = sb.ToString();
            }
        }

        private List<StringWrapper> individualsPools;
        internal ReadOnlyCollection<StringWrapper> IndividualPools
        {
            get { return new ReadOnlyCollection<StringWrapper>(individualsPools); }
            //set 
            //{ 
            //    individualsPool = value;
            //    if (individualsPool != null)
            //    {
            //        label1.DataBindings.Add("Text", individualsPool, "GermPlasm", true, DataSourceUpdateMode.Never);
            //    }

            //}
        }

        public SsC_GermPlasmCtrl()
        {
            InitializeComponent();
            //   this.kryptonLabel1.Text = "";
            this.obCtrls = new List<SsC_ObservationControl>();
            this.individualsPools = new List<StringWrapper>();

            this.stringWrapperBindingSource.DataSource = individualsPools;
            // this.individualsPools.ItemAdded += new EventHandler<ItemEventArgs<IndividualsPool>>(individualsPools_ItemAdded);

        }

        internal void AddPool(StringWrapper poolId)
        {
            if (!individualsPools.Contains(poolId))
                individualsPools.Add(poolId);

            //stringWrapperBindingSource.ResetBindings(false);
            SetLabelContent();
        }


        internal void RemovePool()
        {
            if (individualsPools.Count > 0)
            {
                if (stringWrapperBindingSource.Current != null)
                    stringWrapperBindingSource.RemoveCurrent();
                else
                {
                    individualsPools.RemoveAt(individualsPools.Count - 1);
                }
            }

            stringWrapperBindingSource.ResetBindings(false);
        }

        //void individualsPools_ItemAdded(object sender, ItemEventArgs<IndividualsPool> e)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    sb.Append(this.kryptonLabel1.Text);
        //    if (sb.Length > 0)
        //        sb.Append("\r\n");
        //    sb.Append(e.Item.GermPlasm);
        //    this.kryptonLabel1.Text = sb.ToString();
        //}

        private void kryptonButtonAddObservation_Click(object sender, EventArgs e)
        {

            SsC_ObservationControl c = new SsC_ObservationControl();
            c.CreatorCtrl = this;
            // c.Observation.IndividualsPool = this.individualsPool;
            this.obCtrls.Add(c);
            if (ObservationCtrlAdded != null)
                ObservationCtrlAdded(this, new ObservationControlEventArgs(c));

        }

        private void kryptonButtonRemoveObservation_Click(object sender, EventArgs e)
        {
            if (this.obCtrls.Count > 0)
            {
                SsC_ObservationControl c = this.obCtrls[this.obCtrls.Count - 1];
                this.obCtrls.RemoveAt(this.obCtrls.Count - 1);
                if (ObservationCtrlRemoved != null)
                    ObservationCtrlRemoved(this, new ObservationControlEventArgs(c));
            }
        }

        public event EventHandler<ObservationControlEventArgs> ObservationCtrlAdded;
        public event EventHandler<ObservationControlEventArgs> ObservationCtrlRemoved;



    }



    public class ObservationControlEventArgs : EventArgs
    {
        private SsC_ObservationControl control;
        public SsC_ObservationControl Control
        {
            get { return control; }
        }

        public ObservationControlEventArgs(SsC_ObservationControl c)
        {
            this.control = c;
        }

    }
}
